﻿using Abp.Application.Services.Dto;
using Abp.Web.Mvc.Authorization;
using Akso.Authorization;
using Akso.Codes;
using Akso.Enums;
using Akso.Schools;
using Akso.Web.Areas.BasicDataModule.Models.Schools;
using Akso.Web.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace Akso.Web.Areas.BasicDataModule.Controllers
{
    [AbpMvcAuthorize(AppPermissions.Pages_BasicDataModule_Schools)]
    public class SchoolsController : AbpZeroTemplateControllerBase
    {
        // GET: BasicDataModule/Schools
        private readonly ISchoolAppService _schoolsAppService;
        private readonly ICodeAppService _codesAppService;

        public SchoolsController(ISchoolAppService schoolsAppService, ICodeAppService codesAppService)
        {
            _schoolsAppService = schoolsAppService;
            _codesAppService = codesAppService;
        }
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 为新增、编辑数据页面加载数据，弹出修改页面框。
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AbpMvcAuthorize(AppPermissions.Pages_BasicDataModule_Schools_AddRoot, AppPermissions.Pages_BasicDataModule_Schools_Edit)]
        public async Task<PartialViewResult> CreateOrEditModal(Guid? id,Guid? parentId)
        {
            var output = await _schoolsAppService.GetObjectForEdit(new NullableIdDto<Guid> { Id = id });
            if (parentId != null)
                output.School.ParentId = parentId;
            var deptFunctionList = _codesAppService.GetAllCodesByGroupKey(EnumCollection.EnumCode.DEP_TYPE.ToString()).Items.Select(p => new ComboboxItemDto(p.Id.ToString(), p.CodeName)).ToList();
            deptFunctionList.Insert(0, new ComboboxItemDto("", ""));
            if(id!=null)
            {
                foreach(var item in deptFunctionList)
                {
                    if (item.Value == output.School.FunctionId.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
            }
            var viewModel = new CreateOrEditSchoolModalViewModel(output);
            viewModel.GetDeptFunctionList = deptFunctionList;
            return PartialView("_CreateOrEditModal", viewModel);
        }
    }
}